package utils;

import entity.Token;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TokenUtil {

    private static List<Token> allToken = new ArrayList<>();

    static {
        Collections.addAll(allToken,
                new Token("标识符", 1)
        );

        Collections.addAll(allToken,
                new Token("char", 101),
                new Token("short", 102),
                new Token("int", 103),
                new Token("long", 104),
                new Token("float", 105),
                new Token("double", 106),
                new Token("unsigned", 107),
                new Token("signed", 108),
                new Token("struct", 109),
                new Token("union", 110),
                new Token("enum", 111),
                new Token("extern", 112),
                new Token("static", 113),
                new Token("if", 114),
                new Token("else", 115),
                new Token("switch", 116),
                new Token("for", 117),
                new Token("while", 118),
                new Token("do", 119),
                new Token("case", 120),
                new Token("break", 121),
                new Token("continue", 122),
                new Token("goto", 123),
                new Token("return", 124),
                new Token("default", 125),
                new Token("auto", 126),
                new Token("register", 127),
                new Token("volatile", 128),
                new Token("extern", 129),
                new Token("const", 130),
                new Token("typedef", 131),
                new Token("sizeof", 132)
        );

        Collections.addAll(allToken,
                new Token("整数常量", 201),
                new Token("实数常量", 202),
                new Token("布尔常量", 203),
                new Token("字符常量", 204),
                new Token("字符串常量", 205)
        );

        Collections.addAll(allToken,
                new Token("+", 301),
                new Token("-", 302),
                new Token("*", 303),
                new Token("/", 304),
                new Token("%", 305),
                new Token("++", 306),
                new Token("=", 307),
                new Token("==", 308),
                new Token("!=", 309),
                new Token(">", 310),
                new Token(">=", 311),
                new Token("<", 312),
                new Token("<=", 313),
                new Token("&&", 314),
                new Token("||", 315),
                new Token("!", 316),
                new Token("+=", 317),
                new Token("-=", 318),
                new Token("*=", 319),
                new Token("/=", 320),
                new Token("&", 321),
                new Token("|", 322),
                new Token(">>", 323),
                new Token("<<", 324),
                new Token("<<=", 325),
                new Token(">>=", 326)
        );

        Collections.addAll(allToken,
                new Token(";", 401),
                new Token(",", 402),
                new Token("(", 403),
                new Token(")", 404),
                new Token("{", 405),
                new Token("}", 406),
                new Token("[", 407),
                new Token("]", 408),
                new Token(":", 409),
                new Token(".", 410)
        );
    }

    public static List<Token> getAllToken(){
        return allToken;
    }

    public static int getTokenCode(String word){
        for (Token token : allToken) {
            if(token.getWord().equals(word)){
                return token.getCode();
            }
        }
        return -1;
    }

    public static String getTokenType(int code){
        if(code == 1){
            return "标识符";
        }
        if (code > 100 && code < 200){
            return "关键字";
        }
        if (code == 201){
            return "整数常量";
        }
        if (code == 202){
            return "实数常量";
        }
        if (code == 203){
            return "布尔常量";
        }
        if (code == 204){
            return "字符常量";
        }
        if (code == 205){
            return "字符串常量";
        }
        if (code > 300 && code < 400){
            return "运算符";
        }
        if (code > 400 && code < 500){
            return "界符";
        }
        return "未知";
    }

    public static boolean isKeyWord(String word){
        for (Token token : allToken) {
            if(token.getWord().equals(word)){
                if(token.getCode() > 100 && token.getCode() < 200){
                    return true;
                }else {
                    return false;
                }
            }
        }
        return false;
    }


    public static boolean isSignalOperator(char ch){
        if(ch == '+' || ch == '-' || ch == '*' || ch == '%' || ch == '>' || ch == '<' || ch == '=' || ch == '|' || ch == '&' || ch == '!'){
           return true;
        }
        return false;
    }

    public static boolean isBoundary(char ch){
        int code = getTokenCode(String.valueOf(ch));
        if(code > 400 && code < 500){
            return true;
        }
        return false;
    }

}
